<?php
//
//	file: installer/inc_color.php
//	begin: 08/05/2007
//	$Author: christopher $
//	$Revision: 29 $
//	$Date: 2008-07-19 15:59:35 +0000 (Sat, 19 Jul 2008) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)

if ( !defined('IN_PHPBB') || !defined('IN_INSTALL') )
{
	die('Hack attempt !');
	exit;
}

class color_install
{
	function color_install()
	{
		global $config, $parms_def;

		// includes
		include_once($config->url('includes/aucp/class_color'));
		include_once($config->url('includes/aucp/class_groups'));
		lang_include('lang_CH_install_color');

		// parms (not set is : type => TYPE_INT, default => 0)
		$parms_def += array(
			'copv' => array('type' => TYPE_NO_HTML, 'default' => ''),
			'dcolor' => array('type' => TYPE_NO_HTML, 'default' => ''),
			'cond' => array('type' => TYPE_INT, 'default' => 0),
			'comd' => array('type' => TYPE_INT, 'default' => 0),
		);

		return;
	}

	function start()
	{
		global $part_script, $page;

		// Simple mod identification
		$part_script[] = sprintf($page->lang('CO_welcome_explain'), COLOR_CURRENT_VERSION);

		return;
	}

	function prev_vers()
	{
		global $db, $page, $parms;

		// check
		$parms['copv'] = false;

		// current line
		$parms['copv'] = get_var('mod_aucp_color');

		// AGCM line ->1.2.5b
		$parms['copv'] = ($parms['copv'] === false) ? get_var('mod_advanced_group_color_management') : $parms['copv'];

		// AGCM line -> 1.2.4
		$parms['copv'] = ($parms['copv'] === false) ? get_var('advanced_group_color_management') : $parms['copv'];

		if ($parms['copv'])
		{
			$page->output(sprintf($page->lang('CO_previous_version'),$parms['copv']));
		}

		return;
	}

	function db()
	{
		global $schemas, $parms;

		// install schema
		$parms['cond'] = $parms['comd'] = false;
		switch ( $parms['copv'] )
		{
			case '1.2.4':
				$schemas[] = 'color_125';
			case '1.2.5':
			case '1.2.5a':
			case '1.2.5b':
				$parms['comd'] = true;
			case '2.0.0':
				break;

			// new install
			default:
				$schemas[] = 'color_ch';
				$parms['cond'] = true;
				break;
		}

		return;
	}

	function migrate()
	{
		global $parms, $page, $db, $config;

		if (!$parms['comd'])
		{
			return;
		}

		if ($parms['copv'] == '1.2.4')
		{
			$this->up_124();
		}

		$config->set('aucp_color_check', $config->data['agcm_check'], true);
		$config->set('aucp_color_time', $config->data['agcm_time']*$config->data['agcm_value'], true);
		del_var('agcm_check');
		del_var('agcm_time');
		del_var('agcm_value');
		del_var('mod_advanced_group_color_management');
		del_var('advanced_group_color_management');

		$colors->read(true);

		$page->output('CO_migrate_succeed');

		return;
	}

	function up_124()
	{
		global $parms, $page, $db, $config;

		// add session group
		$sql = 'INSERT INTO ' . GROUPS_TABLE . ' (group_type, group_name, group_description, group_moderator, group_single_user, group_status, group_user_id) VALUES (1, \'Group_session\', \'Group_session_desc\', 0, 1, 3, 0)';
		$db->sql_query($sql, false, __LINE__, __FILE__, false);
		define('GROUP_SESSION', $db->sql_nextid());
		$config->set('group_session', GROUP_SESSION, true);

		// get group ids
		$groups = $group_list->list_groups();
		$parms['dcolor'] = implode(', ', $groups);

		// pull all color info from themes table
		$sql = 'SELECT *
				FROM ' . THEMES_TABLE;
		$result = $db->sql_query($sql, false, __LINE__, __FILE__, false);
		if ( $result === false )
		{
			$page->error(sprintf($page->lang('IN_sql_already_done'), $sql));
		}
		$failed = array();

		// go through all themes and add colors to new colors table
		$sql = 'INSERT INTO ' . COLOR_TABLE . '(group_id, color_code, themes_id, hover_bold, hover_italic, hover_underline, bold, italic, underline) VALUES (%s, %s, $s, 0, 0, 0, 0, 0, 0)';
		// iterate through each theme
		while($row = $db->sql_fetchrow($result))
		{
			// session group
			if($row['session_time'])
			{
				$insert = sprintf($sql, GROUP_SESSION, $row['session_time'], $row['themes_id']);
				$db->sql_query($sql, false, __LINE__, __FILE__, false);
			}

			// anonymous group
			if($row['g0'])
			{
				$insert = sprintf($sql, GROUP_ANONYMOUS, $row['g0'], $row['themes_id']);
				$db->sql_query($sql, false, __LINE__, __FILE__, false);
			}

			// vanilla registered group
			if($row['g'])
			{
				$insert = sprintf($sql, GROUP_REGISTERED, $row['g'], $row['themes_id']);
				$db->sql_query($sql, false, __LINE__, __FILE__, false);
			}

			// everyone else
			foreach($groups as $id)
			{
				if($row['g' . $id])
				{
					$insert = sprintf($sql, 'g' . $id, $row['g' . $id], $row['themes_id']);
					$db->sql_query($sql, false, __LINE__, __FILE__, false);
				}
			}
		}
		$db->sql_freeresult($result);

		$page->output($failed ? sprintf($page->lang('IN_sql_already_done'), implode('<br />', $failed)) : 'CO_old_data_succeed');

		$failed = array();
		$drop_fields = explode(', ', $parms['dcolor']);
		$drop_fields[] = 'g0';
		foreach($drop_fields as $drop)
		{
			$sql = 'ALTER TABLE ' . THEMES_TABLE . ' DROP g' . $drop;
			$db->sql_query($sql, false, __LINE__, __FILE__, false);
		}
		$page->output($failed ? sprintf($page->lang('IN_sql_already_done'), implode('<br />', $failed)) : 'CO_old_tables_succeed');

		return;
	}

	function init_data()
	{
		global $parms, $db, $page, $config, $colors;

		if ( !$parms['cond'] )
		{
			return;
		}

		// add session group
		$sql = 'INSERT INTO ' . GROUPS_TABLE . ' (group_type, group_name, group_description, group_moderator, group_single_user, group_status, group_user_id) VALUES (1, \'Group_session\', \'Group_session_desc\', 0, 1, 3, 0)';
		$db->sql_query($sql, false, __LINE__, __FILE__, false);
		define('GROUP_SESSION', $db->sql_nextid());
		$config->set('group_session', GROUP_SESSION, true);

		// set all users to registered group except anonymous
		$sql = 'ALTER TABLE ' . USERS_TABLE . ' ADD COLUMN \'user_group_id\' MEDIUMINT(8) UNSIGNED DEFAULT ' . GROUP_REGISTERED;
		$db->sql_query($sql, false, __LINE__, __FILE__, false);
		$sql = 'UPDATE TABLE ' . USERS_TABLE . ' SET user_group_id = ' . GROUP_REGISTERED;
		$db->sql_query($sql, false, __LINE__, __FILE__, false);
		$sql = 'UPDATE TABLE ' . USERS_TABLE . ' SET user_group_id = ' . GROUP_ANONYMOUS . ' WHERE user_id = ' . ANONYMOUS;
		$db->sql_query($sql, false, __LINE__, __FILE__, false);

		// build colors table
		$colors->read(true);

		$config->set('aucp_color_check', 0, true);
		$config->set('aucp_color_time', 86400, true);

		return;
	}

	function patch()
	{
		global $config, $colors;

		$config->set('mod_aucp_color', COLOR_CURRENT_VERSION, true);

		return;
	}

	function fin()
	{
		global $page, $parms;

		$page->output(empty($parms['copv']) ? 'CO_install_done' : 'CO_install_upgraded');

		return;
	}
}

$parts[] = 'color_install';

?>